package com.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.component.domain.MDicti;
import com.component.domain.vo.DictiVo;
import com.component.service.MDictiService;
import com.system.common.Result;
import com.system.security.domain.SelfUserEntity;
import com.system.util.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Classname DictiApi
 * @Description TODO
 * @Date 2023/9/24 2:31
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController
@RequestMapping("dicti")
public class DictiApi {

    @Autowired
    private MDictiService mDictiService;

    @PostMapping("add")
    public JSONObject add(@Validated @RequestBody DictiVo vo){
        MDicti mDicti=mDictiService.getById(vo.getPid());
        MDicti obj=new MDicti();
        if(mDicti == null){
            obj.setPid(0);
            obj.setLevel(0);
            if(!StringUtils.isNotBlank(vo.getDictKey())){
                return Result.errorInfo("字典key不得为空");
            }
            MDicti key=mDictiService.getByKey(vo.getDictKey());
            if(key!=null){
                return Result.errorInfo("字典key已存在");
            }
            obj.setDictKey(vo.getDictKey());
            if(!StringUtils.isNotBlank(vo.getDictName())){
                return Result.errorInfo("字典名称不得为空");
            }
            obj.setDictName(vo.getDictName());
        }else {
            obj.setPid(mDicti.getId());
            obj.setLevel(1);
            if(!StringUtils.isNotBlank(vo.getLabelKey())){
                return Result.errorInfo("字典的labelkey不得为空");
            }
            MDicti key=mDictiService.getByLabelKey(mDicti.getPid().toString(),vo.getLabelKey());
            if(key!=null){
                return Result.errorInfo("字典的labelkey已存在");
            }
            obj.setLabelKey(vo.getLabelKey());
            if(!StringUtils.isNotBlank(vo.getLabelName())){
                return Result.errorInfo("字典的label名称不得为空");
            }
            obj.setLabelName(vo.getLabelName());
            if(!StringUtils.isNotBlank(vo.getLabelVal())){
                return Result.errorInfo("字典的label值不得为空");
            }
            obj.setLabelVal(vo.getLabelVal());
        }
        obj.setCreatedate(new Date());
        SelfUserEntity userDetails = SecurityUtil.getUserInfo();
        obj.setUserid(userDetails.getUserId().toString());
        mDictiService.save(obj);
        return Result.successInfo("新增成功");
    }


    @GetMapping("parentpage")
    public JSONObject parentPage(@RequestParam("pageNum")Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize){
        IPage<MDicti> page=mDictiService.parentPage(pageNum,pageSize);
        return Result.successInfo(page);
    }

    @GetMapping("page")
    public JSONObject page(@RequestParam("pid")String pid,
                           @RequestParam("pageNum")Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize){
        IPage<MDicti> page=mDictiService.page(pid,pageNum, pageSize);
        return Result.successInfo(page);
    }

    @PostMapping("delete")
    public JSONObject delete(@RequestBody JSONObject json){
        if(json.get("id")==null || "".equals(json.get("id"))){
            return Result.errorInfo("删除的id不得为空");
        }
        String id=json.get("id").toString();
        MDicti mDicti = mDictiService.getById(id);
        if(mDicti == null){
            return Result.errorInfo("删除的数据不存在");
        }
        System.out.println(JSONObject.toJSONString(mDicti));
        //代表是父级
        if(mDicti.getLevel()==0){
            boolean exist=mDictiService.existsList(mDicti.getId().toString());
            if(exist){
                return Result.errorInfo(mDicti.getDictName()+"下面存在字典项,不允许删除");
            }
        }
        mDictiService.removeById(mDicti.getId());
        return Result.successInfo("删除成功");
    }

    @PostMapping("/update")
    public JSONObject update(@Validated @RequestBody DictiVo dictiVo){
        if(dictiVo.getId()==null){
            return Result.errorInfo("id不得为空");
        }
        MDicti mDicti=mDictiService.getById(dictiVo.getId());
        if(mDicti==null){
            return Result.errorInfo("更新的数据不存在");
        }
        if(mDicti.getLevel()==0){
            if(dictiVo.getDictName()!=null){
                mDicti.setDictName(dictiVo.getDictName());
            }
            if(dictiVo.getDictKey()!=null){
                MDicti key=mDictiService.getByKey(dictiVo.getDictKey());
                if(key!=null){
                    return Result.errorInfo(key.getDictName()+"已存在");
                }
                mDicti.setDictKey(dictiVo.getDictKey());
            }
        }else {
            if(dictiVo.getLabelKey()!=null){
                MDicti key=mDictiService.getByLabelKey(mDicti.getPid().toString(),dictiVo.getLabelKey());
                if(key != null){
                    return Result.errorInfo(key.getLabelKey()+"已存在");
                }
                mDicti.setLabelVal(dictiVo.getLabelKey());
            }
            if(dictiVo.getLabelName()!=null){
                mDicti.setLabelName(dictiVo.getLabelName());
            }
            if(dictiVo.getLabelVal()!=null){
                mDicti.setLabelVal(dictiVo.getLabelVal());
            }
        }
        mDictiService.updateById(mDicti);
        return Result.successInfo("修改成功");
    }
}
